/*
 * @lc app=leetcode.cn id=43 lang=cpp
 *
 * [43] 字符串相乘
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    string multiply(string num1, string num2)
    {
        if (num1[0] == '0' || num2[0] == '0')
            return "0";

        char result[1024] = {0};
        memset(result, 0, sizeof(result));
        if (num1.length() > num2.length())
        {
            num1.swap(num2);
        }

        //保证num1.length() < num2.length()

        int k = 1023;
        for (int i = num1.length() - 1; i >= 0; --i)
        {
            char a = num1[i];
            k = 1023 - (num1.length() - 1 - i);
            // cout << "left: " << a << endl;
            // cout << "right: " << num2 << endl;
            for (int j = num2.length() - 1; j >= 0; --j, --k)
            {
                result[k] += (a - '0') * (num2[j] - '0');
                if (result[k] > 9)
                {
                    result[k - 1] += result[k] / 10;
                    result[k] %= 10;
                }
                // cout << char(result[k] + '0');
            }
            if (result[k] > 9)
            {
                result[k - 1] += result[k] / 10;
                result[k] %= 10;
            }
            // cout << (char)(result[k] + '0');
            // if(result[k-1])
            //     cout << (char)(result[k-1] + '0');
            // cout << endl;
        }

        k = 0;
        while (result[k] == 0)
            k++;
        memcpy(result, result + k, 1024 - k);
        for (int i = 0; i < 1024 - k; i++)
        {
            result[i] += '0';
        }
        result[1024 - k] = 0;
        return result;
    }
    //""123"\n"456""
};
// @lc code=end
